package com.mbatis.leeCode;

/**
 * @Classname InsertWz
 * @Date 2021/6/9 10:45 搜索插入位置
 * @Created by zhang.yuming
 */
public class InsertWz {

    public static void main(String[] args) {
        int[] array = {1,3,5,6,8,9,11,14,15,16,17,21,24,35,56};
        int target = 1;
//        int i = iwz(array,target,arrayLength);
//        System.out.println("i="+i);

        int si =  searchInsert(array,target);
        System.out.println("si="+si);
    }

    private static int searchInsert(int[] nums,int target) {
        int n = nums.length;
        int left = 0, right = n - 1, ans=0;
        while (left <= right) {
            int mid = ((right - left) >> 1) + left;
            if (target <= nums[mid]) {
                ans = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return ans;
    }

    private static int iwz(int[] array, int target, int arrayLength) {
        int tval = 0;
//        int index = 0;
        for (int i = 0; i < arrayLength; i++) {
            if (target == array[i]){
                tval = array[i];
                return tval;
            }else {
                tval++;

            }
        }
        return tval;
    }
}
